package com.itheima.reggie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMpper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishDto;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMpper dishMpper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper ;
    @Override
    public R add(DishDto dishDto) {
        if (dishDto==null){
            return R.error("请添加信息");
        }
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto,dish);
        dishMpper.insert(dish);
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dish.getId());
            dishFlavorMapper.insert(flavor);
        }
        return R.success("添加成功");
    }

    @Override
    public R page(Integer page, Integer pageSize, String name) {
        IPage<Dish> p = new Page<>();
        p.setCurrent(page);
        p.setSize(pageSize);
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(name!=null,Dish::getName,name);
        IPage<Dish> dishIPage = dishMpper.selectPage(p, wrapper);
        String s = JSON.toJSONString(dishIPage.getRecords());
        ArrayList<DishDto> list = new ArrayList<>();
        List<DishDto> dishDto = JSON.parseArray(s, DishDto.class);
        for (DishDto dto : dishDto) {
            Category category = categoryMapper.selectById(dto.getCategoryId());
            dto.setCategoryName(category.getName());
        }
        Page<DishDto> page1 = new Page<>();
        page1.setRecords(dishDto);
        page1.setTotal(dishIPage.getTotal());
        return R.success(page1);
    }

    @Override
    public R fingDish(Long id) {
        Dish dish = dishMpper.selectById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper);
        dishDto.setFlavors(dishFlavors);
        return R.success(dishDto);
    }

    @Override
    public R upData(DishDto dishDto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto,dish);
        dishMpper.updateById(dish);
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dishDto.getId());
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper);
        for (DishFlavor dishFlavor : dishFlavors) {
            dishFlavorMapper.deleteById(dishFlavor.getId());
        }
        for (DishFlavor flavor : dishDto.getFlavors()) {
            flavor.setDishId(dishDto.getId());
            dishFlavorMapper.insert(flavor);
        }
        return R.success("修改成功");
    }

    @Override
    public R removeByIds(Long[] ids) {
            dishMpper.deleteBatchIds(Arrays.asList(ids));
        return R.success("批量删除成功");
    }

    @Override
    public R status(Long[] ids, Integer id) {
        if (id==1){
            //批量起售
//            for (Long aLong : ids) {
////                Dish dish = dishMpper.selectById(aLong);
////                dish.setStatus(1);
////                dishMpper.updateById(dish);
////            }
            Dish dish = new Dish();
            dish.setStatus(id);
            LambdaUpdateWrapper<Dish> wrapper = new LambdaUpdateWrapper<>();
            wrapper.set(Dish::getStatus,id).in(Dish::getId,ids);
            dishMpper.update(dish,wrapper);
            return R.success("批量起售成功");
        }
        if (id==0){
            //批量起售
            for (Long aLong : ids) {
                Dish dish = dishMpper.selectById(aLong);
                dish.setStatus(0);
                dishMpper.updateById(dish);
            }
            return R.success("批量停售成功");
        }
        return R.success("批量操作失败");
    }

    @Override
    public R list(Long categoryId) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,categoryId);
        List<Dish> dishes = dishMpper.selectList(wrapper);
        String s = JSON.toJSONString(dishes);
        List<DishDto> dishDtos = JSON.parseArray(s, DishDto.class);
        for (DishDto dishDto : dishDtos) {
            LambdaQueryWrapper<DishFlavor> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(DishFlavor::getDishId,dishDto.getId());
            List<DishFlavor> list = dishFlavorMapper.selectList(wrapper2);
            dishDto.setFlavors(list);
        }
        return R.success(dishDtos);
    }
}
